import { Controller, Get, Post, Put, Del, Inject, Body, Param, Query } from '@midwayjs/decorator';
import { OtaTaskService } from '../../service/firmware/ota_task.service';
import { resBuild } from '@utils/resBuild';
// import { OtaTaskDto } from '@dto/firmware/ota_task.dto';
@Controller('/ota_task')
export class OtaTaskController {
  @Inject()
  otaTaskService: OtaTaskService;


  // **新增 OTA 任务**
  @Post('/create')
  async create(@Body() body) {
    // console.log('传参', body)
    try {
      await this.otaTaskService.createOtaTask(body);
      // console.log('result', result)
      // console.log('任务创建成功')
      return resBuild.success();
    } catch (error) {
      // console.log('err0', error)
      return resBuild.fail('新增任务失败', error.message);
    }
  }

  // **获取所有 OTA 操作日志**
  @Get('/list')
  async list(@Query() params) {
    try {
      const data = await this.otaTaskService.getAllOtaTasks(params);
      // console.log('result', data)
      return resBuild.data(data);

    } catch (err) {
      console.log('err1', err)
      return resBuild.fail('获取失败', err);
    }


  }


  // **获取单个 OTA 操作日志**
  @Get('/:id')
  async getById(@Param('id') id: string) {
    const log = await this.otaTaskService.getOta_TaskById(id);
    if (!log) return { success: false, message: '日志不存在' };
    return { success: true, data: log };
  }

  // **根据网关和手环 MAC 获取 OTA 操作日志**
  @Get('/gateway/:gw_mac/wristband/:wb_mac')
  async getByMacs(@Param('gw_mac') gw_mac: string, @Param('wb_mac') wb_mac: string) {
    const logs = await this.otaTaskService.getOtaTaskByGwAndWb(gw_mac, wb_mac);
    return { success: true, data: logs };
  }

  // **更新 OTA 操作日志**
  @Put('/update/:id')
  async update(@Param('id') id: string, @Body() body) {
    try {
      const updatedLog = await this.otaTaskService.updateOtaTask(id, body);
      if (!updatedLog) return { success: false, message: '更新失败，日志不存在' };
      return { success: true, data: updatedLog };
    } catch (error) {
      return { success: false, message: error.message };
    }
  }

  // **删除 OTA 操作日志**
  @Del('/delete/:id')
  async delete(@Param('id') id: string) {
    try {
      await this.otaTaskService.deleteOtaTask(id);
      return resBuild.success("删除成功")
    } catch (err) {
      // console.log('校验', err)
      return resBuild.fail("删除失败，日志不存在")
    }
  }
}
